<template>
  <view>
    <!--修改企业印章类型-->
    <uni-popup ref="modifySealType">
      <uni-popup-dialog
          type="info"
          cancelText="取消"
          confirmText="确认"
          title="修改印章类型"
          @close="cancel"
          @confirm="submitForm"
          before-close>
        <template v-slot="content">
          <uni-forms ref="form" style="width: 100%;" :modelValue="form" :rules="rules" label-position="top">
            <uni-forms-item label="印章类型" name="sealTypeId" required>
              <uni-data-select placeholder="请选择印章类型" v-model="form.sealTypeId" :localdata="sealTypeList"
                               :clear="false"/>
            </uni-forms-item>
          </uni-forms>
        </template>
      </uni-popup-dialog>
    </uni-popup>

    <!-- 修改印章名称弹出框 -->
    <uni-popup ref="modifySealNameDialog" type="dialog">
      <uni-popup-dialog
          ref="inputClose"
          mode="input"
          title="修改印章名称"
          :value="dataDetails.name"
          placeholder="请输入修改印章名称"
          @confirm="toUpdateName"
      ></uni-popup-dialog>
    </uni-popup>
  </view>
</template>

<script>
import {updateSealCompanyName, updateSealCompanySealType} from "@/api/ec/console/seal/sealCompany";

export default {
  props: {
    // 印章id
    sealId: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      // 印章类型
      sealTypeList: [],
      // 印章详情
      dataDetails: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sealTypeId: {rules: [{required: true, errorMessage: '请选择印章类型'}]},
      },
    };
  },
  methods: {
    /** 打开弹出框：修改印章类型 */
    toOpenModifySealTypeDialog(data, sealTypeList) {
      this.dataDetails = data;
      this.form = {id: data.id, sealTypeId: data.sealTypeId};
      let range = [];
      sealTypeList.forEach((item, index) => {
        range.push({value: parseInt(item.id), text: item.name})
      });
      this.sealTypeList = range;
      this.$refs.modifySealType.open('center')
    },
    /** 取消按钮 */
    cancel() {
      this.$refs.modifySealType.close()
      this.$refs.modifySealNameDialog.close()
    },
    /** 变更企业信息,确定按钮 */
    submitForm() {
      this.$refs.form.validate().then(valid => {
        if (valid) {
          let id = this.form.id;
          let sealTypeId = this.form.sealTypeId;
          const param = {id, sealTypeId}
          this.$modal.loading();
          updateSealCompanySealType(param).then(() => {
            uni.hideLoading()
            this.$modal.msgSuccess("印章类型修改成功！");
            this.cancel();
            this.$emit("success", "");
          }).catch(() => {
            uni.hideLoading()
            this.$modal.msgError("印章类型修改失败，请稍后重试！")
          });
        }
      });
    },
    /** 打开弹出框：修改印章名称 */
    toOpenModifySealNameDialog(data) {
      this.dataDetails = data;
      this.$refs.modifySealNameDialog.open('center')
    },
    /** 修改印章名称 */
    toUpdateName(name) {
      if (name == '' || name == null) {
        this.$modal.msgError("请输入印章名称");
        return;
      }
      if (name == this.dataDetails.name) {
        return;
      }
      let id = this.dataDetails.id;
      const param = {id, name}
      this.$modal.loading();
      updateSealCompanyName(param).then((response) => {
        uni.hideLoading()
        this.cancel()
        this.$emit("success", "");
        this.$modal.msgSuccess("印章名称修改成功");
      }).catch(() => {
        uni.hideLoading()
        this.$modal.msgError("印章名称修改失败，请稍后重试！")
      });
    },
  }
};
</script>
<style scoped lang="scss">

</style>
